// pages/newBuildinghouse/increaseHouse/increaseHouse.js
import p from "../../../utils/platform"
import locationBehavior from '../../../behaviors/location';
import api from "../../../common/api";
import { insertNewHouseLog } from "../../../common/log";
import constant from "../../../common/constant";

const app = getApp()
Page({
  behaviors: [locationBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    city: "",
    list: [],
    placeholder: "去搜索楼盘",
    searchTextValue: "",
    isFirstEnter: true,
    isGoCityPage: false,
    isGoSearchPage: false,
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const searchTextValue = app.globalData.newHouseSearchValue
    this.getCityBe()
      .then((city) => {
        this.setData({
          searchTextValue,
          city
        })

        app.globalData.newHouseSearchValue = ""
        this.fetchHouse()
      })
  },

  fetchHouse() {
    this.setData({
      list: [],
    })
    this.selectComponent('#com-scroll-content').fetchHouse(1, this.apiFetch.bind(this))
  },

  handleData(e) {
    const rows = e.detail
    rows.forEach((item) => {
      if (item.program_features) {
        item.program_features = item.program_features.replace(/[\{|\}]/g, '').split(",").filter(v=>!!v)
      }
      item._buildSize = [item.min_size, item.max_size].filter(v => !!v).map(v => v + 'm²').join("～")
      item._buildSize = item._buildSize ? "建面" + item._buildSize : "暂无面积"
    })
    const result = this.data.list.concat(rows)
    this.setData({
      list: result,
    })

    this.log(result.length)
  },

  apiFetch({ page, pageSize }) {
    return new Promise((resolve, reject) => {
      api.getShareHouse({
        page,
        pageSize: this.data.pageSize || pageSize,
        city: this.data.city,
        resblockName: this.data.searchTextValue

      }, (res) => {
        resolve(res.data)

        if (app.globalData.searchType && this.data.searchTextValue && page == 1) {
          wx.gio('track', constant.SEARCH_SUCCESS, {      
            [constant.SEARCHFROM_VAR]: app.globalData.searchType,
            [constant.IFSEARCHRESULT_VAR]: !!res.data.content.rows.length,
            [constant.SEARCHWORD_VAR]: this.data.searchTextValue,
            [constant.SEARCHSOURCE_VAR]: constant.NEWHOUSE_PAGE_SEARCH,
          })
        }

      }, (res) => {
        reject()
      })
    })
  },

  hanldeGotoSearch() {
    this.data.isGoSearchPage = true
    p.navigateTo({ url: `/pages/vieHouse/commonSearch/commonSearch?placeholder=${this.data.placeholder}&appKey=newHouseSearchValue&searchTextValue=${this.data.searchTextValue}` })
  },

  handleLocation() {
    this.data.isGoCityPage = true
      // p.navigateTo({
      //   url: `/pages/vieHouse/selectCity/selectCity?city=${this.data.city}&api=getWxCity`
      // })
    p.navigateTo({ url: "/pages/home/switchCity/switchCity" })
  },

  hanldeClearSearchValue() {
    this.setData({
      searchTextValue: ""
    })

    this.fetchHouse()
  },

  hanldeAddHouse(e) {
    const id = e.detail
    api.addNewHouse({ hdId: id }, (res) => {
      if (res.data.isSuccess) {
        p.showModal({
          content: '楼盘已添加至您的个人楼盘',
          showCancel: false
        })
        const index = this.data.list.findIndex((item) => {
          return item.id == id
        })

        if (index > -1) {
          this.setData({
            ['list[' + index + '].status']: res.data.content
          })
        }
      }

      insertNewHouseLog({
        logType: "3",
        cityName: this.data.city,
        floorId: id,
        createResult: res.data.isSuccess ? 0 : 1,
        delId: res.data.isSuccess ? res.data.content : null
      })
    })
  },

  handleGoDetail(e) {
    const id = e.currentTarget.dataset.id
    const status = e.currentTarget.dataset.status
    const houdelid = status != 1 ? status : id

    wx.gio('track', constant.CLICKS_SEARCH_RESULT, {      
      [constant.SEARCHFROM_VAR]: app.globalData.searchType,
      [constant.SEARCHWORD_VAR]: this.data.searchTextValue,
      [constant.SEARCHSOURCE_VAR]: constant.NEWHOUSE_PAGE_SEARCH,
    })

    p.navigateTo({ url: `/pages/newBuildinghouse/newRentalHouseDetail/newRentalHouseDetail?houdelid=${houdelid}&isMay=${status!=1}&city=${this.data.city}` })
  },

  log(length) {
    // 表示首次进来
    if (this.data.isFirstEnter) {
      this.data.isFirstEnter = false
      insertNewHouseLog({
        logType: "0",
        cityName: this.data.city,
      })
    }

    // 城市搜索log
    if (this.data.isGoCityPage) {
      this.data.isGoCityPage = false
      insertNewHouseLog({
        logType: "1",
        cityName: this.data.city,
        resultNum: length,
      })
    }

    // 关键词搜索log
    if (this.data.isGoSearchPage) {
      this.data.isGoSearchPage = false
      insertNewHouseLog({
        logType: "2",
        cityName: this.data.city,
        searchContent: this.data.searchTextValue,
        resultNum: length,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.searchType = ""
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})